﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using UI.Lib;
using Components;

namespace UI.ProductManagement
{
    #region
     // 废弃  2011-03-13
    #endregion

    public partial class FrmPot4a00Medit : UI.ParentForm.BaseMedit
    {
        private string[] keys = null;
        public FrmPot4a00Medit(string modulecode, string action, string text, string[] key)
            : base(modulecode, action)
        {
            InitializeComponent();

            this.Text = text;
            keys = key;

            //回写的方法
            this.SaveCallFuntion = "Pot4a00Manager$SaveData"; //保存的方法
            IsAfterSaveCloseWin = false;
            this.allowDtlEmpty = false; //可能用户一次想把全部的记录删除
            if (null != keys)
            {
                LoadData();
                IsAfterSaveCloseWin = true;
            }
        }

        protected override bool Check()
        {
            if ("A" == this.Action)
            {
                System.Collections.Hashtable hs = new System.Collections.Hashtable();
                hs["promocode"] = this.tbSummary.GetValue("promocode");
                hs["%muser"] = UI.Lib.Context.GetAppKeyWhere();
                bool reflt = UI.Lib.Factory.CommonHelper.Entity.DataInData("Pot4a00", hs);
                if (reflt)
                {
                    MessageBox.Show("当前促销代码已经存在！", "提示");
                    this.tbSummary.Focus("promocode");
                    return false;
                }

                //验证开始时间必须要小于结束时间
                DateTime startdate = Convert.ToDateTime(this.tbSummary.GetValue("startdate"));
                DateTime endate = Convert.ToDateTime(this.tbSummary.GetValue("endate"));
                if (startdate.Subtract(endate).Days > 0)
                {
                    MessageBox.Show("【促销开始日】不能大于【促销截止日】！", "提示");
                    this.tbSummary.Focus("startdate");
                    return false;               
                }

                return true;
            }

            return true;
        }

        public override void AfterPopWin(string name, System.Collections.Hashtable data, bool ischange)
        {
            if ("sellercode" == name && ischange) //销售商改变时，清空门店
            {
                this.tbSummary.ClearItem("shopcode");
                this.tbSummary.ClearItem("shopname");
                this.tbSummary.ClearItem("splistid");
            }
        }

        public override bool BeforePopWin(string name, ref string where, ref string[] others)
        {
            if ("sellercode" == name) //销售商
            {
                where = Context.GetAppKeyWhere("usercode");
            }
            else if ("shopcode" == name)//门店
            {
                string sellerid = Validation.ToString(this.tbSummary.GetValue("slistid"));
                if (string.IsNullOrEmpty(sellerid))
                {
                    MessageBox.Show("请先选择销售商！", "提示");
                    this.tbSummary.Focus("sellercode");
                    return false;
                }

                where = string.Format("slistid = '{0}' {1} ", sellerid, Context.GetAppKeyWhere("muser", true));
            }

            return true;
        }

        protected override bool BeforeDtlPopUp(string name, ref string where, ref string[] others)
        {
            if ("productcode" == name)
            {
                where = Context.GetAppKeyWhere("muser");
            }

            return true;
        }

        //加载修改数据
        private void LoadData()
        {
            base.LoadEditData(null, string.Format(" a.promocode ='{0}' AND a.splistid ='{1}' AND a.slistid ='{2}' "
             , this.keys[0], this.keys[1], this.keys[2]));
            base.LoadDtlData(null, string.Format(" a.promocode ='{0}' AND a.splistid ='{1}' AND a.slistid ='{2}' "
                , this.keys[0], this.keys[1], this.keys[2]));
        }
    }
}
